<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
class User extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('Admin_Layout');
        $this->load->model('user/user_model');
    }

    public function index() {
        $this->load->organize();
    }

    public function organize($page = '') {
        $this->mycommon->authAdmin();
        $page = ($page != '') ? $page : 0;
        $max = $this->user_model->num_rows();
        $min = 10;
        $data['num_rows'] = $max;
        $this->load->library('pagination');
        $config['base_url'] = site_url("admin/user/index");
        $config['total_rows'] = $max;
        $config['per_page'] = $min;
        $config['first_link'] = '&lsaquo;';
        $config['last_link'] = '&rsaquo;';
        $config['cur_page'] = $page;
        $config['uri_segment'] = 4;
        $config['cur_tag_open'] = "<a href=#>";
        $config['cur_tag_close'] = "</a>";
        $this->pagination->initialize($config);
        $data['link'] = $this->pagination->create_links();
        $data['lstuser'] = $this->user_model->get_list_user($min, $this->uri->segment($config['uri_segment']));
        $this->admin_layout->view('user/list_view', $data);
        $this->load->database();
    }

    public function add() {
        $this->mycommon->authAdmin();
        $this->load->model('user/user_model');
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $data = array();
        if (isset($_POST['submit'])) {
            $this->form_validation->set_rules('username', 'username', 'required|min_length[5]|max_length[15]');
            $this->form_validation->set_rules('email', 'email', 'required|valid_email');
            $this->form_validation->set_rules('username', 'Username', 'required|callback_username_check');
            $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[50]');
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            if ($this->form_validation->run($this) == FALSE) {
                
            } else {
                $acc = $this->input->post("username");
                $pwd = md5($this->input->post("password"));
                $email = $this->input->post('email');
                $name = $this->input->post("name");

                $data['acc'] = $acc;
                $data['pwd'] = $pwd;
                $data['email'] = $email;
                $data['name'] = $name;
                $this->user_model->add_user($data);
                redirect(site_url('admin/user/organize'));
            }
        }

        $this->admin_layout->view('user/add_view', $data);
    }

    function edit($id) {
        $this->mycommon->authAdmin();
        $this->load->library('form_validation');
        $this->load->model('user/user_model');
        $data = array();
        $this->form_validation->set_rules('email_address', 'email_address', 'required');
        $this->form_validation->set_rules('username', 'username', 'required');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $data = array();
        if ($id) {
            $id = $this->uri->segment(4);
            $userEdit = $this->user_model->edit_user($id);
        }
        if ($this->input->post('submit')) {
            if (count($_POST) > 0) {
                $id = $this->uri->segment(4, 0);
                $acc = $this->input->post("username");
                $email = $this->input->post("email");
                $name = $this->input->post("name");

                $array = array(
                    'id' => $id,
                    'acc' => $acc,
                    'email' => $email,
                    'name' => $name
                );
                if ($this->form_validation->run() != FALSE) {
                    $this->user_model->update_user($id, $acc, $mail, $name);
                    redirect(site_url('admin/user/organize'));
                }
            }
        }
        $data['user'] = $userEdit[0];
        $this->admin_layout->view('user/edit_view', $data);
    }

    function delete() {
        $this->mycommon->authAdmin();
        $user_id = $this->input->get('id');
        $this->user_model->delete_user($user_id);
        redirect(base_url() . 'admin/user/organize');
    }

}

?>
